package com.cheng.mybatis.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cheng.mybatis.domain.User;

public class LoginIntecepter implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		System.out.println("拦截器开始工作...");

		String uri = request.getRequestURI();
		if (uri.indexOf("login.action") > 0) {
			System.out.println("login");
			return true;
		}

		User user = (User) request.getSession().getAttribute("user");
		if (user != null) {
			return true;
		}

		// 重新登陆
		response.sendRedirect(request.getContextPath() + "/login.jsp");

		return false;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
